package com.opdar.athena.web.service;

import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.mapper.UserMapper;
import com.opdar.athena.errors.ResultError;
import com.opdar.platform.utils.MD5;
import com.opdar.platform.utils.SHA1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * Created by shiju on 2017/1/27.
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public boolean isExist(String email){
        UserEntity user = new UserEntity();
        user.setEmail(email);
        return userMapper.count(user) > 0;
    }

    @Transactional(rollbackFor = Throwable.class)
    public UserEntity register(String userName, String password,String appId){
        UserEntity user = new UserEntity();
        user.setUserName(userName);
        user.setAppId(appId);
        if(userMapper.count(user) > 0){
            return null;
        }else{
            user.setId(UUID.randomUUID().toString());
            user.setSalt(UUID.randomUUID().toString());
            password = SHA1.encrypt(MD5.encrypt(password).concat(user.getSalt()));
            user.setPassword(password);
            user.setRole("NORMAL");
            user.setCreateTime(new Timestamp(System.currentTimeMillis()));
            user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            userMapper.insert(user);
            return user;
        }
    }

    public UserEntity changePwd(String userName, String password, String appId) {
        UserEntity user = new UserEntity();
        user.setUserName(userName);
        user.setAppId(appId);
        if(userMapper.count(user) > 0){
            UserEntity where = new UserEntity();
            where.setAppId(appId);
            where.setUserName(userName);
            UserEntity update = new UserEntity();
            update.setSalt(UUID.randomUUID().toString());
            password = SHA1.encrypt(MD5.encrypt(password).concat(update.getSalt()));
            update.setPassword(password);
            userMapper.update(update,where);
            return user;
        }else{
            return null;
        }
    }

    public UserEntity authByUserName(String userName,String userPwd,String appId){
        UserEntity query = new UserEntity();
        query.setUserName(userName);
        query.setAppId(appId);
        //根据用户名查询密码盐
        UserEntity user = getUserByPassword(userPwd, query);
        if (user == null) return null;
        return user;
    }

    private UserEntity getUserByPassword(String userPwd, UserEntity query) {
        UserEntity user = userMapper.selectOne(query);
        if(user == null || user.getSalt() == null){
            return null;
        }
        String salt = user.getSalt();
        //根据盐计算正确密码
        userPwd = SHA1.encrypt(MD5.encrypt(userPwd).concat(salt));
        query.setPassword(userPwd);
        user = userMapper.selectOne(query);
        //去除密码信息并返回
        user.setPassword(null);
        user.setSalt(null);
        return user;
    }

    public UserEntity auth(String email,String userPwd){
        UserEntity query = new UserEntity();
        query.setEmail(email);
        //根据用户名查询密码盐
        UserEntity user = getUserByPassword(userPwd, query);
        if (user == null) return null;
        return user;
    }

    public boolean insert(UserEntity insert){
        insert.setId(UUID.randomUUID().toString());
        insert.setSalt(UUID.randomUUID().toString());
        insert.setForbid(0);
        insert.setCreateTime(new Timestamp(System.currentTimeMillis()));
        insert.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        //根据用户名查询密码盐
        String salt = insert.getSalt();
        //根据盐计算正确密码
        String userPwd = SHA1.encrypt(MD5.encrypt(insert.getPassword()).concat(salt));
        insert.setPassword(userPwd);

        UserEntity query = new UserEntity();
        query.setEmail(insert.getEmail());
        int ret = userMapper.count(query);
        if(ret > 0){
            throw new ResultError("账号已存在，请返回后重新输入.");
        }

        ret = userMapper.insert(insert);
        return ret > 0;
    }

    public boolean update(UserEntity update, UserEntity where) {
        return userMapper.update(update,where)>0;
    }

    public boolean exist(String id) {
        UserEntity query = new UserEntity();
        query.setId(id);
        return userMapper.count(query)>0;
    }

    public List<UserEntity> selectSystemUserList() {
        return userMapper.selectSystemUserList();
    }
}
